import json
from pathlib import Path
from loguru import logger



HTTP_SUCCESS = 200

BLOCK_LINE_SIZE = 500

HEADER_FILE_DETECT_LINES = 200

TEMP_FILE_DIR = Path('/Catalog/Errorsniffer/temp')

MAX_DETECT_LINES_PER_FILE = 5000

CODE_HEADER_FILE_EXTENSIONS = ('.h', '.hpp', '.hxx', '.hh')
CODE_FILE_EXTENSIONS = ('.h', '.hpp', '.hxx', '.hh','.c', '.cpp', '.cxx', '.cc')


# 读取配置文件
def load_config():
    """
    读取配置文件
    
    Returns:
        dict: 配置信息字典
    """
    try:
        config_path = Path(__file__).parent / 'config.json'
        with open(config_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        logger.error("错误: 配置文件未找到！")
        return {}
    except json.JSONDecodeError:
        logger.error("错误: 配置文件格式无效！")
        return {}


class AppConfig:
    """应用配置类"""
    def __init__(self):
        self._config_loaded = False
        self.gerrit_url = None
        self.username = None
        self.password = None
        self.reportspath = None
        self.dify_api_server = None
        self.dify_api_key = None
        self.dify_api_test_key = None
        self.v5_repo = None
        self.v6_repo = None
        self.v35_repo = None
        self.v4_repo = None
        self.algo_people = None
        
    def load_config(self):
        """加载配置文件"""
        config = load_config()
        self.gerrit_url = config['gerrit']['url']
        self.username = config['gerrit']['username']
        self.password = config['gerrit']['password']
        self.reportspath = config['gerrit']['reportspath']
        self.dify_api_server = config['dify']['DIFY_API_SERVER']
        self.dify_api_key = config['dify']['DIFY_API_KEY']
        self.dify_api_test_key = config['dify']['DIFY_API_TEST_KEY']
        self.v5_repo = config['v5repo']
        self.v6_repo = config['v6repo']
        self.v35_repo = config['v35repo']
        self.v4_repo = config['v4repo']
        self.algo_people = config['algo_people']
        self._config_loaded = True

    def ensure_loaded(self):
        """确保配置已加载"""
        if not self._config_loaded:
            self.load_config()


